package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.MyCouponAdapter;
import com.luck.easyfoodbatch.mvp.mine.MyVouchersModel;
import com.luck.easyfoodbatch.mvp.mine.MyVouchersPresenter;
import com.luck.easyfoodbatch.mvp.mine.MyVouchersView;
import com.luck.easyfoodbatch.mvp.model.MyCouponModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCouponActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, MyVouchersView {

    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    @BindView(R.id.tv_more_overdue)
    TextView tvMoreOverdue;
    @BindView(R.id.tv_exchange)
    TextView tv_exchange;
    private MyCouponAdapter adapter;
    private List<MyCouponModel.DataBean.ModelsBean> list = new ArrayList<>();
    private MyVouchersModel myVouchersModel;
    private int totalPage = 1;
    private int page = 1;
    private int isRefresh = 0;// 0普通请求，1下拉刷新，2上拉加载
    private int effective = 1;
    private int type = 0;// 0是我的优惠券，1是订单优惠券
    private String order_id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            setContentView(R.layout.activity_my_coupon, "我的优惠券", "不使用优惠券", MODE_BACK_NAVIGATION);
        } else {
            setContentView(R.layout.activity_my_coupon, "我的优惠券", MODE_BACK_NAVIGATION);
        }

        ButterKnife.bind(this);
        order_id = getIntent().getStringExtra("order_id");
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new MyCouponAdapter(mContext);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                MyCouponModel.DataBean.ModelsBean modelsBean = list.get(position);
                if (type == 1) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("vid", modelsBean.getId());
                    bundle.putString("money", modelsBean.getDec_money() + "");
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        showProgressDialog(getString(R.string.app_loading));
        myVouchersModel = new MyVouchersPresenter(this, mContext);
        myVouchersModel.myVouchers(effective, page, type, order_id);
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
    }

    @Override
    public void rightClick() {
        super.rightClick();
        Intent intent = new Intent();
        intent.putExtra("vid", "0");
        intent.putExtra("dec_money", "");
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onLoadMore() {
        //刷新
        isRefresh = 1;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        myVouchersModel.myVouchers(effective, page, type, order_id);
    }

    @Override
    public void onRefresh() {
        isRefresh = 0;
        page = 1;
        myVouchersModel.myVouchers(effective, page, type, order_id);
    }

    @OnClick({R.id.tv_more_overdue, R.id.tv_exchange})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.tv_more_overdue:
                startAct(OverdueCouponActivity.class);
                break;
            case R.id.tv_exchange:
                intent.setClass(mContext, ExchangeActivity.class);
                startActivityForResult(intent, 200);
                break;
        }

    }

    @Override
    public void loginMyVouchers(MyCouponModel model) {
        if (model.getStatus() == HttpUtil.RESULT_OK) {
            totalPage = model.getData().getTotalPage();
            // 下拉刷新 清空集合
            switch (isRefresh) {
                case 0:
                    list.clear();
                    list = model.getData().getModels();
                    if (list == null) {
                        list = new ArrayList<>();
                    }
                    break;
                case 1:
                    // 下拉刷新
                    list.addAll(model.getData().getModels());
                    if (list == null) {
                        list = new ArrayList<>();
                    }
                    break;
            }
            adapter.clear();
            adapter.addAll(list);
            adapter.notifyDataSetChanged();
        } else {
            T.showToast(mContext, model.getMsg());
        }
    }

    @Override
    public void httpStatusError() {

    }


    @Override
    public void hideLoading() {
        hideShowProgress();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    isRefresh = 0;
                    page = 1;
                    myVouchersModel.myVouchers(effective, page, type, order_id);
                    break;
            }
        }
    }
}
